﻿using GalaSoft.MvvmLight.Messaging;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Windows;

namespace wp8nop.Model
{
    public class DataService : IDataService
    {
        public DataService()
        {
            Carrello = new ObservableCollection<ProdottoViewModel>();
        }

        public void GetAllProductsDisplayedOnHomePage(Action<ObservableCollection<ProdottoViewModel>, Exception> callback)
        {
            
            nopService.NopServiceClient client = new nopService.NopServiceClient();
            client.GetProductsCompleted += (s, e) =>
            {
                if (e.Error != null)
                    callback(null, e.Error);

                ObservableCollection<ProdottoViewModel> ret = new ObservableCollection<ProdottoViewModel>();
                e.Result.products.ForEach( x=> ret.Add( new ProdottoViewModel(x) ));
                callback(ret, e.Error);
            };
            client.GetProductsAsync();
        }


        public System.Collections.Generic.List<Categoria> GetCategorie()
        {
            List<Categoria> catList = new List<Categoria>();
            catList.Add(new Categoria()
            {
                Descrizione = "Orologi",
                IDCategoria = 415,
                Ricerca = "orologi",
                Image = "/Assets/Ico/orologi.jpg"
            });
            catList.Add(new Categoria()
            {
                Descrizione = "Gioielli",
                IDCategoria = 417,
                Ricerca = "gioielli",
                Image = "/Assets/Ico/gioielli.jpg"

            });
            catList.Add(new Categoria()
            {
                Descrizione = "Occhiali da sole",
                IDCategoria = 431,
                Ricerca = "occhiali",
                Image = "/Assets/Ico/occhiali.jpg"
              
            });
            catList.Add(new Categoria()
            {
                Descrizione = "Borse",
                IDCategoria = 441,
                Ricerca = "borse",
                Image = "/Assets/Ico/borse.jpg"
            });
            catList.Add(new Categoria()
            {
                Descrizione = "Novità",
                IDCategoria = 463,
                Ricerca = "novita",
                Image = "/Assets/Ico/new.jpg"
            });
            catList.Add(new Categoria()
            {
                Descrizione = "Breil",
                IDCategoria = 0,
                Ricerca = "breil",
                Image = "/Assets/Ico/breil.jpg"
            });            
            catList.Add(new Categoria()
            {
                Descrizione = "Just Cavalli",
                IDCategoria = 0,
                Ricerca = "cavalli",
                Image = "/Assets/Ico/cavalli.jpg"
            });            
            catList.Add(new Categoria()
            {
                Descrizione = "Casio",
                IDCategoria = 0,
                Ricerca = "casio",
                Image = "/Assets/Ico/casio.jpg"
            });
            catList.Add(new Categoria()
            {
                Descrizione = "Sector",
                IDCategoria = 0,
                Ricerca = "sector",
                Image = "/Assets/Ico/sector.jpg"
            });
            catList.Add(new Categoria()
            {
                Descrizione = "Morellato",
                IDCategoria = 0,
                Ricerca = "morellato",
                Image = "/Assets/Ico/morellato.jpg"
            });
            catList.Add(new Categoria()
            {
                Descrizione = "Guess",
                IDCategoria = 0,
                Ricerca = "guess",
                Image = "/Assets/Ico/guess.jpg"
            });
            catList.Add(new Categoria()
            {
                Descrizione = "Chronotech",
                IDCategoria = 0,
                Ricerca = "chronotech",
                Image = "/Assets/Ico/chronotech.jpg"
            });

            return catList;
        }


        public List<Categoria> GetSubCategorie(string cat)
        {
            List<Categoria> catList = new List<Categoria>();

            if (cat == "orologi")
            {
                catList.Add(new Categoria("Donna", null, 416, 0, 0));
                catList.Add(new Categoria("Uomo", null, 425, 0, 0));
                catList.Add(new Categoria("Bambino", null, 426, 0, 0));
                catList.Add(new Categoria("Unisex", null, 432, 0, 0));
                catList.Add(new Categoria("da 0 a 40€", null, 415, 0, 40));
                catList.Add(new Categoria("da 40 a 80€", null, 415, 40, 80));
                catList.Add(new Categoria("da 80 a 150€", null, 415, 80, 150));
                catList.Add(new Categoria("da 150€ in poi", null, 415, 150, 0));
                catList.Add(new Categoria("Breil", "Breil", 415, 0, 0));
                catList.Add(new Categoria("Cavalli", "Cavalli", 415, 0, 0));		
                catList.Add(new Categoria("Casio", "Casio", 415, 0, 0));	
                catList.Add(new Categoria("Sector", "Sector", 415, 0, 0));
                catList.Add(new Categoria("D&G", "D&G", 415, 0, 0));
                catList.Add(new Categoria("Guess", "Guess", 415, 0, 0));
                catList.Add(new Categoria("Ck", "Ck", 415, 0, 0));
                catList.Add(new Categoria("Alviero Martini", "Alviero Martini", 415, 0, 0));
                catList.Add(new Categoria("Ferragamo", "Ferragamo", 415, 0, 0));
                catList.Add(new Categoria("Nautica", "Nautica", 415, 0, 0));
                catList.Add(new Categoria("Morellato", "Morellato", 415, 0, 0));
                catList.Add(new Categoria("Timex", "Timex", 415, 0, 0));
                catList.Add(new Categoria("Swiss Legend", "Swiss Legend", 415, 0, 0));
                catList.Add(new Categoria("Marc Ecko", "Marc Ecko", 415, 0, 0));
                catList.Add(new Categoria("Tissot", "Tissot", 415, 0, 0));
                catList.Add(new Categoria("Michael Kors", "Michael Kors", 415, 0, 0));
                catList.Add(new Categoria("Orient", "Orient", 415, 0, 0));
                catList.Add(new Categoria("Swatch", "Swatch", 415, 0, 0));
                catList.Add(new Categoria("Philip Watch", "Philip Watch", 415, 0, 0));
                catList.Add(new Categoria("Lorus", "Lorus", 415, 0, 0));
                catList.Add(new Categoria("Lorenz", "Lorenz", 415, 0, 0));
                catList.Add(new Categoria("Esprit", "Esprit", 415, 0, 0));
                catList.Add(new Categoria("Pryngeps", "Pryngeps", 415, 0, 0));        
            }
            else if( cat == "gioielli")
            {

                catList.Add(new Categoria("Collane", null, 421, 0, 0));
                catList.Add(new Categoria("Bracciali", null, 423, 0, 0));
                catList.Add(new Categoria("Anelli", null, 418, 0, 0));
                catList.Add(new Categoria("Orecchini", null, 419, 0, 0));
                catList.Add(new Categoria("da 0 a 30€", null, 417, 0, 30));
                catList.Add(new Categoria("da 30 a 50€", null, 417, 30, 50));
                catList.Add(new Categoria("da 50 a 80€", null, 417, 50, 80));
                catList.Add(new Categoria("da 50 a 80€", null, 417, 50, 80));
                catList.Add(new Categoria("da 80 a 150€", null, 417, 80, 150));
                catList.Add(new Categoria("da 150€ in poi", null, 417, 150, 0));

                catList.Add(new Categoria("Morellato", "Morellato", 417, 0, 0));
                catList.Add(new Categoria("Breil", "Breil", 417, 0, 0));
                catList.Add(new Categoria("Bliss", "Bliss", 417, 0, 0));
                catList.Add(new Categoria("D&G", "D&G", 417, 0, 0));
                catList.Add(new Categoria("Ck", "Ck", 417, 0, 0));
                catList.Add(new Categoria("Sector", "Sector", 417, 0, 0));
                catList.Add(new Categoria("Miss Sixty", "Sixty", 417, 0, 0));
                catList.Add(new Categoria("Choice", "Choice", 417, 0, 0));
                catList.Add(new Categoria("Sweet Years", "Sweet Years", 417, 0, 0));
                catList.Add(new Categoria("Molecole", "Molecole", 417, 0, 0));
                catList.Add(new Categoria("Ducati Jewels", "Ducati", 417, 0, 0));
                catList.Add(new Categoria("Ferrari Jewels", "Ferrari", 417, 0, 0));
               
            }
            else if( cat == "occhiali")
            {
                catList.Add(new Categoria("da 0 a 40€", null, 431, 0, 40));
                catList.Add(new Categoria("da 40 a 80€", null, 431, 40, 80));
                catList.Add(new Categoria("da 80 a 150€", null, 431, 80, 150));
                catList.Add(new Categoria("da 150€ in poi", null, 431, 150, 0));                 
                catList.Add(new Categoria("Gucci", "Gucci", 431, 0, 0));
                catList.Add(new Categoria("Giorgio Armani", "Armani", 431, 0, 0));
                catList.Add(new Categoria("Diesel", "Diesel", 431, 0, 0));
                catList.Add(new Categoria("Guess", "Guess", 431, 0, 0));
                catList.Add(new Categoria("Hogan", "Hogan", 431, 0, 0));
                catList.Add(new Categoria("Benetton", "Benetton", 431, 0, 0));
                catList.Add(new Categoria("Calvin Klein", "Klein", 431, 0, 0));
                catList.Add(new Categoria("Nike", "Nike", 431, 0, 0));
                catList.Add(new Categoria("Roberto Cavalli", "Cavalli", 431, 0, 0));
                catList.Add(new Categoria("Valentino", "Valentino", 431, 0, 0));
                catList.Add(new Categoria("Byblos", "Byblos", 431, 0, 0));
                catList.Add(new Categoria("Tom Ford", "Form", 431, 0, 0));
                catList.Add(new Categoria("Swatch", "Swatch", 431, 0, 0));
            }
            else if (cat == "borse")
            {
                catList.Add(new Categoria("da 0 a 40€", null, 441, 0, 40));
                catList.Add(new Categoria("da 40 a 80€", null, 441, 40, 80));
                catList.Add(new Categoria("da 80 a 150€", null, 441, 80, 150));
                catList.Add(new Categoria("da 150€ in poi", null, 441, 150, 0)); 
				catList.Add(new Categoria("Guess", "Guess", 441, 0, 0));
				catList.Add(new Categoria("Morellato", "Morellato", 441, 0, 0));
				catList.Add(new Categoria("Diesel", "Diesel", 441, 0, 0));
				catList.Add(new Categoria("Naj Oleari", "Naj", 441, 0, 0));
				catList.Add(new Categoria("Gas", "Gas", 441, 0, 0));
				catList.Add(new Categoria("Eastpack", "Eastpack", 441, 0, 0));
				catList.Add(new Categoria("Lollipops", "Lollipops", 441, 0, 0));
            }

            return catList;
        }


        public void GetProducts(Action<List<ProdottoViewModel>, Exception> callback, Categoria selCat, string SearchString, int Page)
        {
            Debug.WriteLine("Get prodotti func");
            nopService.NopServiceClient client = new nopService.NopServiceClient();
            client.GetProductsByCategoryCompleted += (s, e) => {
                Debug.WriteLine("Get prodotti completed");

                if (e.Error != null)
                    callback(null, e.Error);

                Debug.WriteLine("Get prodotti completed 1");
                List<ProdottoViewModel> ret = new List<ProdottoViewModel>();
                e.Result.products.ForEach(x => ret.Add(new ProdottoViewModel(x)));
                Debug.WriteLine("Get prodotti callback 1");
                callback(ret, e.Error);
            };
            if (!string.IsNullOrEmpty(SearchString))
            {
                Debug.WriteLine("Get product func search {0}", SearchString);
                client.GetProductsByCategoryAsync("0", "0", "0", "", SearchString, "1", Page.ToString());
            }
            else
            {
                string ricerca = selCat.Ricerca;
                if (ricerca == "novita")
                    ricerca = "-1";
                Debug.WriteLine("Get product func {0}, {1}, {2}, {3} ", selCat.IDCategoria.ToString(), selCat.PriceMin.ToString(), selCat.PriceMax.ToString(), selCat.Ricerca);
                client.GetProductsByCategoryAsync(selCat.IDCategoria.ToString(), selCat.PriceMin.ToString(), selCat.PriceMax.ToString(), "", ricerca, "1", Page.ToString());
            }
        }


        public ObservableCollection<ProdottoViewModel> Carrello { get; set; }



        public void AddToCart(ProdottoViewModel prod)
        {
            if (Carrello.Contains(prod))
                prod.Quantita++;
            else
                Carrello.Add(prod);

            Messenger.Default.Send(new NotificationMessage("UpdatedCarrello"));
        }


        public double GetTotaleCarrello()
        {
            double tot = 0;
            foreach (var c in Carrello)
            {
                tot = c.Model.price * c.Quantita;
            }

            return tot;
        }

        public double GetSpedizioni()
        {
            int contr = 0;
            if (App.TipoPagamento == "contrassegno")
                contr = 4;

            return GetTotaleCarrello() >= 60 ? contr : 6.9 + contr;
        }


        public void ClearCarrello()
        {
            Carrello.Clear();
            Messenger.Default.Send(new NotificationMessage("UpdatedCarrello"));
        }
    }
}